package com.hairline.service;

import com.hairline.bean.*;
import com.hairline.mapper.*;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;


    @Override
    public List<User> queryAllUserInfo(Integer page, Integer limit, String username, String mobile, String addTime, String order) {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUsernameLike("%" + username + "%").andMobileLike("%" + mobile + "%");
        //排序
        userExample.setOrderByClause(addTime + " " + order);
        List<User> users = userMapper.selectByExample(userExample);


        return users;
    }

    @Autowired
    AddressMapper addressMapper;
    @Override
    public List<Address> queryAllReceiveAddress(Integer page, Integer limit, String name, String userId, String addTime, String order) {

        AddressExample addressExample = new AddressExample();
        AddressExample.Criteria criteria = addressExample.createCriteria();
        //排序
        addressExample.setOrderByClause(addTime + " " +order);
        if (userId == null || "".equals(userId)) {
            criteria.andNameLike("%"+ name + "%");
        }else {
            criteria.andNameLike("%"+ name + "%").andUserIdEqualTo(Integer.valueOf(userId));
        }
        List<Address> addresses = addressMapper.selectByExample(addressExample);
        return addresses;
    }

    @Autowired
    CollectMapper collectMapper;
    @Override
    public List<Collect> queryAllUserCollections(Integer page, Integer limit, String valueId, String userId, String addTime, String order) {
        CollectExample collectExample = new CollectExample();
        CollectExample.Criteria criteria = collectExample.createCriteria();

        collectExample.setOrderByClause(addTime + " " +order);
        if (valueId != null && !"".equals(valueId)) {
            criteria.andValueIdEqualTo(Integer.valueOf(valueId));
        }
        if (userId != null && !"".equals(userId)) {
            criteria.andUserIdEqualTo(Integer.valueOf(userId));
        }
        //criteria.andValueIdEqualTo(Integer.valueOf(valueId)).andUserIdEqualTo(Integer.valueOf(userId));

        List<Collect> collects = collectMapper.selectByExample(collectExample);
        return collects;
    }

    @Autowired
    FootprintMapper footprintMapper;
    @Override
    public List<Footprint> queryAllFootprint(Integer page, Integer limit, String goodsId, String userId, String addTime, String order) {
        FootprintExample footprintExample = new FootprintExample();
        FootprintExample.Criteria criteria = footprintExample.createCriteria();

        footprintExample.setOrderByClause(addTime + " " +order);
        if (goodsId != null && !"".equals(goodsId)) {
            criteria.andGoodsIdEqualTo(Integer.valueOf(goodsId));
        }
        if (userId != null && !"".equals(userId)) {
            criteria.andUserIdEqualTo(Integer.valueOf(userId));
        }
        List<Footprint> footprints = footprintMapper.selectByExample(footprintExample);
        return footprints;
    }

    @Autowired
    SearchHistoryMapper searchHistoryMapper;
    @Override
    public List<SearchHistory> queryAllUserSearchHistory(Integer page, Integer limit, String keyword, String userId, String addTime, String order) {
        SearchHistoryExample searchHistoryExample = new SearchHistoryExample();
        SearchHistoryExample.Criteria criteria = searchHistoryExample.createCriteria();

        searchHistoryExample.setOrderByClause(addTime + " " +order);
        if (keyword != null && !"".equals(keyword)) {
            criteria.andKeywordLike("%" + keyword + "%");
        }
        if (userId != null && !"".equals(userId)) {
            criteria.andUserIdEqualTo(Integer.valueOf(userId));
        }
        List<SearchHistory> searchHistories = searchHistoryMapper.selectByExample(searchHistoryExample);
        return searchHistories;
    }
    @Autowired
    FeedbackMapper feedbackMapper;
    @Override
    public List<Feedback> queryAllUserFeedBack(Integer page, Integer limit, String username, String id, String addTime, String order) {
        FeedbackExample feedbackExample = new FeedbackExample();
        FeedbackExample.Criteria criteria = feedbackExample.createCriteria();

        feedbackExample.setOrderByClause(addTime + " " +order);
        if (username != null && !"".equals(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }
        if (id != null && !"".equals(id)) {
            criteria.andIdEqualTo(Integer.valueOf(id));
        }
        List<Feedback> feedbacks = feedbackMapper.selectByExample(feedbackExample);
        return feedbacks;
    }

    @Override
    public Integer count() {
        return userMapper.count();
    }

    @Override
    public void updateLoginTime(Integer id, String ip) {
        userMapper.updateLoginTime(id, ip);
    }
}
